package com.sdp.wx.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_emp.entity.WxEmp;
import com.sdp.wx.db.wx_emp.service.IWxEmpService;
import com.sdp.wx.db.wx_emp_work.entity.WxEmpWork;
import com.sdp.wx.db.wx_emp_work.service.IWxEmpWorkService;
import com.sdp.wx.db.wx_emp_work_attendance.entity.WxEmpWorkAttendance;
import com.sdp.wx.db.wx_emp_work_attendance.service.IWxEmpWorkAttendanceService;
import com.sdp.wx.db.wx_emp_work_attendance_rule.entity.WxEmpWorkAttendanceRule;
import com.sdp.wx.db.wx_emp_work_attendance_rule.service.IWxEmpWorkAttendanceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-18
 */
@RestController
@RequestMapping("/WxEmpWorkAttendanceController")
public class WxEmpWorkAttendanceController {
    @Autowired
    private IWxEmpWorkAttendanceService iWxEmpWorkAttendanceService;
    @Autowired
    private IWxEmpWorkAttendanceRuleService iWxEmpWorkAttendanceRuleService;
    @Autowired
    private IWxEmpService iWxEmpService;
    @Autowired
    private IWxEmpWorkService iWxEmpWorkService;
    @Autowired
    private Query query;

    @PostMapping(value = "/page")
    public Result<?> page(@RequestBody DataCenter dataCenter) {
        return Result.ok(query.page(iWxEmpWorkAttendanceService, WxEmpWorkAttendance.class, dataCenter));
    }
    @PostMapping(value = "/save")
    public Result<?> page(@RequestBody JSONObject data) {
        WxEmpWorkAttendance wxEmpWorkAttendance = data.getObject("formData", WxEmpWorkAttendance.class);
        List<WxEmpWorkAttendanceRule> recordList = JSONObject.parseArray(JSON.toJSONString(data.get("recordList")), WxEmpWorkAttendanceRule.class);
        if(StringUtils.isEmpty(wxEmpWorkAttendance.getKeyId())){
            //保存规则主表
            wxEmpWorkAttendance.setCreateTime(LocalDateTime.now());
            iWxEmpWorkAttendanceService.save(wxEmpWorkAttendance);
        }else{
            // 更新规则
            iWxEmpWorkAttendanceService.updateById(wxEmpWorkAttendance);
        }
        // 删除旧数据
        QueryWrapper<WxEmpWorkAttendanceRule> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WxEmpWorkAttendanceRule::getWorkAttendanceId,wxEmpWorkAttendance.getKeyId());
        iWxEmpWorkAttendanceRuleService.remove(queryWrapper);
        // 保存新数据
        for(WxEmpWorkAttendanceRule wxEmpWorkAttendanceRule:recordList){
            wxEmpWorkAttendanceRule.setWorkAttendanceId(wxEmpWorkAttendance.getKeyId());
        }
        iWxEmpWorkAttendanceRuleService.saveBatch(recordList);
        // 修正符合规则人员信息
        QueryWrapper<WxEmpWork> wxEmpWorkQueryWrapper=new QueryWrapper<>();
        wxEmpWorkQueryWrapper.lambda().eq(WxEmpWork::getWorkAttendanceId,wxEmpWorkAttendance.getKeyId());
        iWxEmpWorkService.remove(wxEmpWorkQueryWrapper);
        // 增加符合规则人员信息
        if(!StringUtils.isEmpty(wxEmpWorkAttendance.getWorkOrgNo())){
            QueryWrapper<WxEmp> queryWrapper2=new QueryWrapper<>();
            List<String> orgNo= Arrays.asList(wxEmpWorkAttendance.getWorkOrgNo().split(","));
            queryWrapper2.lambda().in(WxEmp::getOrgNo,orgNo);
            List<WxEmp>  wxEmpList= iWxEmpService.list(queryWrapper2);
            if(wxEmpList.size()>0){
                List<WxEmpWork> wxEmpWorkList=new ArrayList<>();
                for(WxEmp wxEmp:wxEmpList){
                    WxEmpWork wxEmpWork=new WxEmpWork();
                    wxEmpWork.setWorkName(wxEmpWorkAttendance.getWorkName());
                    wxEmpWork.setWorkEmpNo(wxEmp.getEmpNo());
                    wxEmpWork.setWorkOrgNo(wxEmp.getOrgNo());
                    wxEmpWork.setCreateTime(LocalDateTime.now());
                    wxEmpWork.setWorkEmpName(wxEmp.getEmpName());
                    wxEmpWork.setWorkAttendanceId(wxEmpWorkAttendance.getKeyId());
                    wxEmpWorkList.add(wxEmpWork);
                }
                iWxEmpWorkService.saveBatch(wxEmpWorkList);
            }
        }
        return Result.ok();
    }
}

